let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    common: true,
    headType: true,
    id: 0, //发票id
    billTypeId: 1, // 发票类型：个人：1，公司：2，增值税：3
    userId: '',
    billPhone: '', // 收票人手机号
    billAddress: '', // 收票人地址
    billLinkMan: '', // 个人发票联系人
    companyName: '', // 公司名称
    discernmentNum: '', // 公司税号
    registerPhone: '', // 增值税注册手机号
    registerAddress: '', // 增值税注册地址
    bank: '', // 增值税开户银行
    account: '', // 增值税银行账号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userId = wx.getStorageSync("userId");
    this.setData({userId});
    // 查看发票详情
    this.getInvoice();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  alert(msg) {
    wx.showToast({
      title: msg,
      image: '../../assets/images/warning.png',
      duration: 1500
    })
  },
  // 选择发票类型
  switchType (e) {
    let typeId = e.currentTarget.dataset.id;
    this.setData({billTypeId: e.currentTarget.dataset.typeid});
    if (typeId == 0) {
      this.setData({common: true, headType: true});
    } else if (typeId == 1) {
      this.setData({common: false});
    }
    // 查看发票详情
    this.getInvoice();
  },
  // 选择发票抬头
  chooseHead (e) {
    console.log(e);
    if (e.detail.value == 0) {
      this.setData({headType: true});
    } else if (e.detail.value == 1) {
      this.setData({headType: false});
    }
  },
  chooseTypeId (e) {
    console.log(e);
    this.setData({ billTypeId: e.currentTarget.dataset.typeid });
    // 查看发票详情
    this.getInvoice();
  },
  // 增加或修改发票
  addInvoice () {
    let params = {};
    // 个人发票
    if (this.data.common && this.data.headType) {
      if (this.data.billLinkMan == '') {
        this.alert("请输入个人姓名");
        return false;
      }
      if (this.data.billPhone == '') {
        this.alert("请输入收票人手机号");
        return false;
      }
      if (this.data.billAdress == '') {
        this.alert("请输入收票人地址");
        return false;
      }
      params.id = this.data.id;
      params.userId = this.data.userId;
      params.billTypeId = this.data.billTypeId;
      params.billLinkMan = this.data.billLinkMan;
      params.billPhone = this.data.billPhone;
      params.billAddress = this.data.billAddress;
      App.HttpService.addInvoice(params).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
          })
          wx.setStorageSync('billTypeId', this.data.billTypeId);
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000);
        } else {
          wx.showToast({
            title: res.data.msg,
            image: '../../assets/images/warning.png'
          });
          return false;
        }
      });
      // 公司发票
    } else if (this.data.common && !this.data.headType) {
      if (this.data.companyName == '') {
        this.alert("请输入公司名称");
        return false;
      }
      if (this.data.discernmentNum == '') {
        this.alert("请输入公司税号");
        return false;
      }
      if (this.data.billLinkMan == '') {
        this.alert("请输入收票人姓名");
        return false;
      }
      if (this.data.billPhone == '') {
        this.alert("请输入收票人手机号");
        return false;
      }
      if (this.data.billAddress == '') {
        this.alert("请输入收票人地址");
        return false;
      }
      params.id = this.data.id;
      params.userId = this.data.userId;
      params.billTypeId = this.data.billTypeId;
      params.billLinkMan = this.data.billLinkMan;
      params.companyName = this.data.companyName;
      params.discernmentNum = this.data.discernmentNum;
      params.billPhone = this.data.billPhone;
      params.billAddress = this.data.billAddress;
      App.HttpService.addInvoice(params).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
          })
          wx.setStorageSync('billTypeId', this.data.billTypeId);
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000);
        }
      });
      // 增值税发票
    } else if (!this.data.common) {
      
      if (this.data.companyName == '') {
        this.alert("请输入公司名称");
        return false;
      }
      if (this.data.discernmentNum == '') {
        this.alert("请输入公司税号");
        return false;
      }
      if (this.data.registerAddress == '') {
        this.alert("请输入注册地址");
        return false;
      }
      if (this.data.registerPhone == '') {
        this.alert("请输入注册电话");
        return false;
      }
      if (this.data.bank == '') {
        this.alert("请输入开户银行");
        return false;
      }
      if (this.data.account == '') {
        this.alert("请输入银行账户");
        return false;
      }
      if (this.data.billLinkMan == '') {
        this.alert("请输入收票人姓名");
        return false;
      }
      if (this.data.billPhone == '') {
        this.alert("请输入收票人手机号");
        return false;
      }
      if (this.data.billAddress == '') {
        this.alert("请输入收票人地址");
        return false;
      }
      params.id = this.data.id;
      params.userId = this.data.userId;
      params.billTypeId = this.data.billTypeId;
      params.companyName = this.data.companyName;
      params.discernmentNum = this.data.discernmentNum;
      params.registerAddress = this.data.registerAddress;
      params.registerPhone = this.data.registerPhone;
      params.bank = this.data.bank;
      params.account = this.data.account;
      params.billLinkMan = this.data.billLinkMan;
      params.billPhone = this.data.billPhone;
      params.billAddress = this.data.billAddress;
      App.HttpService.addInvoice(params).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
          });
          wx.setStorageSync('billTypeId', this.data.billTypeId);
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000);
        }
      });
    }
    
  },
  // 获取发票详情
  getInvoice () {
    App.HttpService.invoiceDetail({ userId: this.data.userId, billType: this.data.billTypeId }).then(res => {
      console.log(res);
      if (res.data.code == 200 && res.data.data.billInfo) {
        let data = res.data.data.billInfo;
        this.setData({
          bank: data.bank,
          account: data.bankAccount,
          billAddress: data.billAddress,
          billLinkMan: data.billLinkMan,
          billPhone: data.billPhone,
          companyName: data.companyName,
          discernmentNum: data.discernmentNum,
          id: data.id,
          registerAddress: data.registerAddress,
          registerPhone: data.registerPhone
        });
        
      }else {
        this.setData({
          bank: '',
          account: '',
          billAddress:'',
          billLinkMan: '',
          billPhone: '',
          companyName: '',
          discernmentNum: '',
          id: '',
          registerAddress: '',
          registerPhone: ''
        });
      }
      console.log(this.data.billLinkMan);
    });
  },
  // 输入个人姓名
  billLinkMan (e) {
    this.setData({billLinkMan: e.detail.value});
  },
  // 输入收票人手机号
  billPhone (e) {
    this.setData({billPhone: e.detail.value});
  },
  // 输入收票人地址
  billAddress (e) {
    this.setData({billAddress: e.detail.value});
  },
  // 输入公司名称
  companyName (e) {
    this.setData({companyName: e.detail.value});
  },
  // 输入公司税号
  discernmentNum (e) {
    this.setData({discernmentNum: e.detail.value});
  },
  // 输入注册地址
  registerAddress (e) {
    this.setData({ registerAddress: e.detail.value});
  },
  // 输入注册电话
  registerPhone (e) {
    this.setData({ registerPhone: e.detail.value});
  },
  // 输入开户银行
  bank (e) {
    this.setData({bank: e.detail.value});
  },
  // 输入银行账户
  account (e) {
    this.setData({account: e.detail.value});
  }
})